package logic

import (
	"context"
	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"
	"house/models/mysql"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseShowLogic {
	return &HouseShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseShowLogic) HouseShow(in *houserpc.HouseShowRequest) (*houserpc.HouseShowResponse, error) {
	// todo: add your logic here and delete this line
	var a []mysql.House
	sx := mysql.DB.Debug().Where(" city = ?", in.City)
	if in.MinPricePrice > 0 {
		sx = sx.Where("price > ?", in.MinPricePrice)
	}
	if in.MaxPrice > 0 {
		sx = sx.Where("price < ?", in.MaxPrice)
	}
	sx.Find(&a)
	var b []*houserpc.HouseShowList
	for _, c := range a {
		list := houserpc.HouseShowList{
			HouseId:   int64(c.ID),
			UserId:    int64(c.UserId),
			Title:     c.Title,
			Price:     float32(c.Price),
			Rent:      float32(c.Rent),
			Area:      int64(c.Area),
			City:      c.City,
			Way:       c.Way,
			StartTime: c.CreatedAt.Format(time.DateTime),
		}
		b = append(b, &list)
	}
	return &houserpc.HouseShowResponse{
		List: b,
	}, nil
}
